<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\index\dao\ConsultingDao;
use app\index\model\UM as UM;

/**
 * Class ConsultingRecentCheckService
 * @package app\index\service
 */
class ConsultingRecentCheckService
{
    // dao
    private ConsultingDao $consultingDao;

    public function __construct(ConsultingDao $consultingDao)
    {
        $this->consultingDao = $consultingDao;
    }

    /**
     * 查询最近支付的聊天室（在有效时间内）
     * @param UM $user
     * @param array $params
     * @return Consulting|null
     */
    public function read(UM $user, array $params): ?Consulting
    {
        $memberId = $user->id;

        return Consulting::where('memberId', $memberId)
            ->where('consultingType', '1')
            ->where('status', '1')
            ->where('isEnd', '0')
            ->where(function ($query) {
                $query->whereOr(function ($query) {
                    $query->where('isChatFreely', '1')
                        ->where('isChatFreelyEndTime', '>', time());
                })
                    ->whereOr('questionsNum', '>', 0);
            })
            // 在畅聊中的，或者有剩余次数的，未结束的
            ->field(['id'])
            ->find();
    }

}
